import 'dart:math';

import 'package:flutter_kit_dsp/flutter_kit_dsp.dart';
import 'package:flutter_kit_plus/flutter_kit.dart';

import '../tool/assets.dart';

class UnitBar extends StatefulWidget {
  final String title;
  const UnitBar({super.key, required this.title});
  @override
  State<UnitBar> createState() => _UnitBarState();
}

class _UnitBarState extends State<UnitBar> {
  List<GroupModel> _group_list = [];

  Widget buildText(GroupModel group, ChannelModel ch) {
    return myTextButton(
        radius: 4,
        color: ch.select ? Color_theme : Colors.transparent,
        borderColor: ch.select ? Color_theme : Colors.black,
        colors: [Colors.transparent, Colors.white.withOpacity(0.2)],
        onTap: () => globalController.chooseChInGroup(_group_list, group, ch, widget.title),
        child: Center(
          child: FittedBox(
            fit: BoxFit.scaleDown, // 会根据父组件自动缩小
            child: Text(
              '${ch.num}',
              style: font40,
              textAlign: TextAlign.center,
              textHeightBehavior: const TextHeightBehavior(
                applyHeightToFirstAscent: false,
                applyHeightToLastDescent: false,
              ),
            ),
          ),
        ));
  }

  @override
  Widget build(BuildContext context) {
    return GetBuilder<GetxGlobalController>(builder: (c) {
      var list = c.rxModel.value.ch_list;
      _group_list = List.generate((list.length / 2).toInt(), (index) {
        return GroupModel(ch1: list[2 * index], ch2: list[2 * index + 1]);
      });

      for (var group in _group_list) {
        if (group.ch1!.select && group.ch2!.select) {
          group.select = true;
        }
      }
      return LayoutBuilder(builder: (BuildContext context, BoxConstraints constraints) {
        double gap = 2;
        double h_item = (constraints.maxWidth - (_group_list.length * 2 - 1) * gap) / _group_list.length / 2;
        h_item = min(constraints.maxHeight, h_item);
        gap = (constraints.maxWidth - (_group_list.length * 2) * h_item) / (_group_list.length * 2 - 1);
        double w_item = (h_item * 2 + gap);
        // int num = (_group_list.length / 2).toInt();
        return GridView.builder(
            physics: const NeverScrollableScrollPhysics(),
            padding: const EdgeInsets.all(0),
            itemCount: _group_list.length,
            gridDelegate: SliverGridDelegateWithFixedCrossAxisCount(
                crossAxisCount: _group_list.length,
                crossAxisSpacing: gap,
                mainAxisSpacing: 5,
                childAspectRatio: w_item / h_item),
            itemBuilder: (BuildContext context, index) {
              GroupModel group = _group_list[index];
              double btn_w = h_item * 2 / 3.0;
              double img_w = h_item / 3.0;
              return Container(
                  child: Stack(
                children: [
                  Align(
                      alignment: Alignment.topCenter,
                      child: myIconButton(
                          height: img_w + 3,
                          iconName: group.select ? assets.unit_select : assets.unit,
                          onTap: () => globalController.chooseGroup(_group_list, group, widget.title))),
                  Positioned(
                      left: w_item / 2 - 10 - btn_w,
                      bottom: 0,
                      width: btn_w,
                      height: btn_w,
                      child: buildText(group, group.ch1!)),
                  Positioned(
                      right: w_item / 2 - 10 - btn_w,
                      bottom: 0,
                      width: btn_w,
                      height: btn_w,
                      child: buildText(group, group.ch2!)),
                ],
              ));
            });
      });
    });
  }
}
